package com.zhangchuchu.neohealth.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangchuchu.neohealth.common.R;
import com.zhangchuchu.neohealth.entity.VisitLog;
import com.zhangchuchu.neohealth.service.VisitLogService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;

/**
 * (VisitLog)控制层
 *
 * @author makejava
 * @since 2025-04-17 09:20:13
 */
@RestController
@RequestMapping("/visitLog")
public class VisitLogController {
    @Resource
    private VisitLogService visitLogService;

    /**
     * 查询所有数据（不分页）
     */
    @GetMapping("/getList")
    public R<List<VisitLog>> selectList() {
        return R.success(visitLogService.list());
    }

    /**
     * 分页查询
     */
    @GetMapping
    public R<Page<VisitLog>> selectAll(Page<VisitLog> page, VisitLog visitLog) {
        return R.success(visitLogService.page(page, new QueryWrapper<>(visitLog)));
    }

    /**
     * 通过主键查询单条数据
     */
    @GetMapping("{id}")
    public R<VisitLog> selectOne(@PathVariable Serializable id) {
        return R.success(visitLogService.getById(id));
    }

    /**
     * 新增数据
     */
    @PostMapping
    public R<Boolean> insert(@RequestBody VisitLog visitLog) {
        return R.success(visitLogService.save(visitLog));
    }

    /**
     * 修改数据
     */
    @PutMapping
    public R<Boolean> update(@RequestBody VisitLog visitLog) {
        return R.success(visitLogService.updateById(visitLog));
    }

    /**
     * 删除数据（批量）
     */
    @DeleteMapping
    public R<Boolean> delete(@RequestParam("idList") List<Integer> idList) {
        return R.success(visitLogService.removeByIds(idList));
    }
    // 1. 登录时记录访问
    @PostMapping("/record")
    public R recordVisit(HttpServletRequest request) {
        visitLogService.recordVisit(request);
        return R.success("访问记录成功");
    }

    // 2. 获取近7天访问数据（参数格式 yyyy-MM-dd）
    @GetMapping("/stats")
    public R<List<Map<String, Object>>> getLast7DaysStats(@RequestParam("date") String dateStr) {
        LocalDate date = LocalDate.parse(dateStr);
        List<Map<String, Object>> stats = visitLogService.getLast7DaysVisits(date);
        return R.success("data", stats);
    }
}
